<template>
  <el-card>
    <template #header>修改密码</template>

    <!-- 主体内容 -->
    <el-form
      :model="passForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="80px"
      class="demo-ruleForm"
      size="small"
    >
      <el-form-item label="原密码" prop="oldPass">
        <el-input
          type="password"
          v-model="passForm.oldPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPass">
        <el-input
          type="password"
          v-model="passForm.newPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="passForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确认修改</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { resetPwd } from '@/api/users.js'

export default {
  data() {
    // 验证原密码是否为空
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入原密码'))
      } else {
        callback()
      }
    }
    // 验证新密码
    var validateNewPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        // 表单的一致性规定，在验证新密码时失去焦点会触发确认密码的校验
        if (this.passForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    // 验证确认密码
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.passForm.newPass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      passForm: {
        oldPass: '',
        newPass: '',
        checkPass: ''
      },
      rules: {
        oldPass: [{ validator: validatePass, trigger: 'blur' }],
        newPass: [{ validator: validateNewPass, trigger: 'blur' }],
        checkPass: [{ validator: validatePass2, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 这里代表校验结果全部通过
          const users = JSON.parse(window.localStorage.getItem('users') || '{}')
          const { code, msg } = await resetPwd({
            member_id: users.member_id,
            access_token: users.access_token,
            oldPassword: this.passForm.oldPass,
            password: this.passForm.newPass,
            repassword: this.passForm.checkPass
          })

          if (code === 1) {
            this.$message.success(msg)
            // 修改密码之后要重新登录
            window.localStorage.clear()
            setTimeout(() => {
              location.reload()
            }, 1500)
          } else {
            this.$message.error(msg)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 350px;
  margin-top: 20px;
}
</style>
